package com.xh.web.util.velocity;



import com.alibaba.fastjson.JSONObject;
import com.xh.web.constant.DbModuleConstants;
import com.xh.web.util.DateUtils;
import com.xh.web.util.GenConstants;
import com.xh.web.util.StringUtils;
import org.apache.velocity.VelocityContext;

import java.util.ArrayList;
import java.util.List;

/**
 * 模板处理工具类
 *
 * @author ruoyi
 */
public class VelocityUtils
{
    /** 项目空间路径 */
    private static final String PROJECT_PATH = "main/java";

    /** mybatis空间路径 */
    private static final String MYBATIS_PATH = "main/resources/mapper";

    /** 默认上级菜单，系统工具 */
    private static final String DEFAULT_PARENT_MENU_ID = "3";

    /**
     * 设置模板变量信息
     *
     * @return 模板列表
     */
    public static VelocityContext prepareContext()
    {

        VelocityContext velocityContext = new VelocityContext();
        //velocityContext.put("basePackage", getPackagePrefix(packageName));
        //velocityContext.put("packageName", packageName);

        velocityContext.put("datetime", DateUtils.getDate());

        return velocityContext;
    }



    public static void setTreeVelocityContext(VelocityContext context)
    {
       // String options = genTable.getOptions();
        String options="";
        JSONObject paramsObj = JSONObject.parseObject(options);
        String treeCode = getTreecode(paramsObj);
        String treeParentCode = getTreeParentCode(paramsObj);
        String treeName = getTreeName(paramsObj);

        context.put("treeCode", treeCode);
        context.put("treeParentCode", treeParentCode);
        context.put("treeName", treeName);
        //context.put("expandColumn", getExpandColumn(genTable));
        if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
        {
            context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
        }
        if (paramsObj.containsKey(GenConstants.TREE_NAME))
        {
            context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME));
        }
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplateList(String dbType)
    {
        List<String> templates = new ArrayList<String>();
        if (dbType.equals(DbModuleConstants.SQL_TABLE_ORACLE)){
            templates.add("vm/sql/table_oracle.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_TABLE_MYSQL)){
            templates.add("vm/sql/table_mysql.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_TABLE_SQLSERVER)){
            templates.add("vm/sql/table_sqlserver.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_TABLE_DM)){
            templates.add("vm/sql/table_dm.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_COLUMN_ORACLE)){
            templates.add("vm/sql/column_oracle.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_COLUMN_MYSQL)){
            templates.add("vm/sql/column_mysql.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_COLUMN_SQLSERVER)){
            templates.add("vm/sql/column_sqlserver.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_COLUMN_DM)){
            templates.add("vm/sql/column_dm.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_INDEX_ORACLE)){
            templates.add("vm/sql/index_oracle.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_INDEX_MYSQL)){
            templates.add("vm/sql/index_mysql.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_INDEX_SQLSERVER)){
            templates.add("vm/sql/index_sqlserver.vm");
        }else if(dbType.equals(DbModuleConstants.SQL_INDEX_DM)){
            templates.add("vm/sql/index_dm.vm");
        }
        return templates;
    }






    public static List<String> getTemplateJavaList(String tplCategory)
    {
        List<String> templates = new ArrayList<String>();
        if (tplCategory.equals(DbModuleConstants.MYBATIS)){
            templates.add("vm/mybatis/domain.vm");
            templates.add("vm/mybatis/controller.vm");
            templates.add("vm/mybatis/service.vm");
            templates.add("vm/mybatis/serviceImpl.vm");
            templates.add("vm/mybatis/mapper.vm");
            templates.add("vm/mybatis/mapper.xml.vm");
        }
        if (tplCategory.equals(DbModuleConstants.MYBATISPLUS)){
            templates.add("vm/mybatisplus/domain.vm");
            templates.add("vm/mybatisplus/controller.vm");
            templates.add("vm/mybatisplus/service.vm");
            templates.add("vm/mybatisplus/serviceImpl.vm");
            templates.add("vm/mybatisplus/mapper.vm");
        }
        if (tplCategory.equals(DbModuleConstants.HIBERNATE)){
            templates.add("vm/hibernate/domain.vm");
            templates.add("vm/hibernate/controller.vm");
            templates.add("vm/hibernate/service.vm");
            templates.add("vm/hibernate/serviceImpl.vm");
            templates.add("vm/hibernate/dao.vm");
            //templates.add("vm/hibernate/hibernate_daoImpl.vm");
        }
        return templates;
    }


    /**
     * 获取包前缀
     *
     * @param packageName 包名称
     * @return 包前缀名称
     */
    /*public static String getPackagePrefix(String packageName)
    {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtils.substring(packageName, 0, lastIndex);
        return basePackage;
    }*/


    /**
     * 获取权限前缀
     *
     * @param moduleName 模块名称
     * @param businessName 业务名称
     * @return 返回权限前缀
     */
    public static String getPermissionPrefix(String moduleName, String businessName)
    {
        return StringUtils.format("{}:{}", moduleName, businessName);
    }

    /**
     * 获取上级菜单ID字段
     *
     * @param paramsObj 生成其他选项
     * @return 上级菜单ID字段
     */
    public static String getParentMenuId(JSONObject paramsObj)
    {
        if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID))
        {
            return paramsObj.getString(GenConstants.PARENT_MENU_ID);
        }
        return DEFAULT_PARENT_MENU_ID;
    }

    /**
     * 获取树编码
     *
     * @param paramsObj 生成其他选项
     * @return 树编码
     */
    public static String getTreecode(JSONObject paramsObj)
    {
        if (paramsObj.containsKey(GenConstants.TREE_CODE))
        {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取树父编码
     *
     * @param paramsObj 生成其他选项
     * @return 树父编码
     */
    public static String getTreeParentCode(JSONObject paramsObj)
    {
        if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
        {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取树名称
     *
     * @param paramsObj 生成其他选项
     * @return 树名称
     */
    public static String getTreeName(JSONObject paramsObj)
    {
        if (paramsObj.containsKey(GenConstants.TREE_NAME))
        {
            return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template,String tableCode)
    {
        // 文件名称
        String fileName = "";

        if (template.contains("sql.vm"))
        {
            fileName = tableCode+ ".sql";
        }
        return fileName;
    }
}
